package cn.stylefeng.guns.modular.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.Feedback;
import cn.stylefeng.guns.modular.entity.FeedbackClass;
import cn.stylefeng.guns.modular.entity.User;
import cn.stylefeng.guns.modular.entity.responseVo.FeedbackListResponseVo;
import cn.stylefeng.guns.modular.mapper.FeedbackClassMapper;
import cn.stylefeng.guns.modular.mapper.FeedbackMapper;
import cn.stylefeng.guns.modular.mapper.UserCompanyMapper;
import cn.stylefeng.guns.modular.mapper.WorkshopMapper;
import cn.stylefeng.guns.modular.service.IFeedbackService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author XiangTianGe
 * @since 2020-04-09
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private UserCompanyMapper userCompanyMapper;

    @Autowired
    private FeedbackClassMapper feedbackClassMapper;

    @Override
    public LayuiPageInfo getList(Page<Feedback> page, String beginTime, String endTime, String keyword, Long type, Integer status) {


        QueryWrapper<Feedback> qw = new QueryWrapper();
        if (!Strings.isNullOrEmpty(beginTime) && !Strings.isNullOrEmpty(endTime)) {
            qw.between("DATE(created_at)", beginTime, endTime);
        }
        if (!Strings.isNullOrEmpty(keyword)) {
            qw.eq("content", keyword);
        }
        if (type != null) {
            qw.eq("class_id", type);
        }
        if (status != null) {
            qw.eq("handle_status", status);
        }

        IPage<Feedback> iPage = feedbackMapper.selectPage(page, qw);
        List<Feedback> records = iPage.getRecords();
        List<FeedbackListResponseVo> collect = records.stream().map(f -> {
            // 查询用户
            User user = userCompanyMapper.selectById(f.getUserId());
            // 查询反馈类型
            FeedbackClass feedbackClass = feedbackClassMapper.selectById(f.getClassId());
            FeedbackListResponseVo responseVo = FeedbackListResponseVo.fromFeedbackListResponseVo(f, user, feedbackClass);
            return responseVo;
        }).collect(Collectors.toList());

        return LayuiPageFactory.createPageInfo(page, collect);
    }

    /**
     * 查看详情
     */
    @Override
    public ModelAndView details(Long id) {

        ModelAndView view = new ModelAndView();

        //  反馈信息
        Feedback feedback = feedbackMapper.selectById(id);
        view.addObject("feedback", feedback);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(feedback.getCreatedAt());
        view.addObject("date", format);

        String picture = feedback.getPicture();
        String[] split = picture.split(",");
        StringBuffer sb = new StringBuffer();

        Arrays.stream(split).forEach(f ->
                sb.append("<img src=" + f + ">"));

        view.addObject("picture", sb);

        // 用户信息
        User user = userCompanyMapper.selectById(feedback.getUserId());
        view.addObject("user", user);

        //  反馈类型
        FeedbackClass feedbackClass = feedbackClassMapper.selectById(feedback.getClassId());

        view.addObject("feedbackClass", feedbackClass);
        return view;
    }

    /**
     * 处理反馈
     */
    @Override
    public void handle(Feedback feedback) {
        feedback.setUpdatedAt(new Date());
        feedbackMapper.updateById(feedback);
    }

    /**
     * 删除反馈
     */
    @Override
    public void delete(Long id) {
        feedbackMapper.deleteById(id);
    }

    /**
     * 跳转反馈首页
     */
    @Override
    public ModelAndView index() {

        List<FeedbackClass> feedbackClasses = feedbackClassMapper.selectList(null);
        StringBuffer sb = new StringBuffer();
        feedbackClasses.forEach(f -> sb.append("<option value=" + f.getClassId() + ">" + f.getClassName() + "</option>"));
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("class", sb);
        return modelAndView;
    }


}
